package cn.ly.link;

import java.util.Scanner;

import cn.ly.link.data.LinkQueue;
import cn.ly.link.data.LinkStack;
import cn.ly.link.order.Node1;
import cn.ly.link.order.OrderLinkList;
import cn.ly.link.two.TwoLinkList;
import cn.ly.util.ArithmeticTool;

public class LinkListApp {
	
	public static void main(String[] args) {
		
		java.util.Iterator it;
		java.util.ArrayList al;
		//testDoubleHeadLinkList();
		//testLinkStack();
		//testLinkQueue();
		//testOrderLinkList();
		//testOrderLinkListByArray();
		testTwoLinkList();
		
		
	}
	
	public static void testTwoLinkList(){
		TwoLinkList two = new TwoLinkList();
		for(int i=0; i<4; i++){
			two.insertFirst(ArithmeticTool.getIntRandom0to100());
		}
		two.displayForward();
		
		for(int i=0; i<4; i++){
			two.insertLast(ArithmeticTool.getIntRandom0to100());
		}
		
		two.displayForward();
		
		two.removeFirst();
		two.displayForward();
		two.removeLast();
		two.displayForward();

		int data = input();
		two.insertAfter(data, 49);
		two.displayForward();
		two.removeKey(49);
		two.displayForward();
		two.displayBackward();
		
	}
	

	public static void testOrderLinkListByArray(){
		Node1[] nArr = new Node1[10];
		for(int i=0; i<10; i++){
			nArr[i] = new Node1(ArithmeticTool.getIntRandom0to100());
		}
		for(int i=0; i<10; i++){
			System.out.print(nArr[i].data + " ");
		}
		System.out.println();
		
		OrderLinkList linkList = new OrderLinkList(nArr);
		linkList.display();
		
		for(int i=0; i<10; i++){
			nArr[i] = new Node1(linkList.remove());
		}
		for(int i=0; i<10; i++){
			System.out.print(nArr[i].data + " ");
		}
		
	}
	
	
	public static void testOrderLinkList(){
		OrderLinkList linkList = new OrderLinkList();
		for(int i=0; i<8; i++){
			linkList.insert(ArithmeticTool.getIntRandom0to100());
		}
		linkList.display();
		System.out.println(linkList.remove());
		linkList.display();
		linkList.insert(40);
		linkList.display();
	}
	
	
	public static void testLinkQueue(){
		LinkQueue<Integer> queue = new LinkQueue<Integer>();
		for(int i=0; i<8; i++){
			queue.insert(ArithmeticTool.getIntRandom0to100());
		}
		
		queue.displayQueue();
		
		System.out.println(queue.remove());
		queue.displayQueue();
		
		queue.insert(60);
		
		queue.displayQueue();
	}
	
	public static void testLinkStack(){
		LinkStack<Integer> stack = new LinkStack<Integer>();
		for(int i=0; i<8; i++){
			stack.push(ArithmeticTool.getIntRandom0to100());
		}
		
		stack.displayStack();
		
		System.out.println(stack.pop());
		
		stack.displayStack();
		
		stack.push(60);
		
		stack.displayStack();
		
	}
	
	
	public static void testDoubleHeadLinkList(){
		DoubleHeadLinkList myLink = new DoubleHeadLinkList();
		for(int i=0; i<10; i++){
			myLink.insertFirst(ArithmeticTool.getIntRandom0to100());
		}

		
		myLink.display();
		Scanner c = new Scanner(System.in);
		int data = 0;
		while(c.hasNext() && ( (data = c.nextInt()) != -1)){
			myLink.insertLast(data);
			myLink.display();
		}
		
		while(c.hasNext() && ( (data = c.nextInt()) != -1)){
			myLink.deleteFirst();
			myLink.display();
		}
		
		
		
	}
	
	public void testLinkList(){
		LinkList myLink = new LinkList();
		for(int i=0; i<10; i++){
			myLink.insertFirst(ArithmeticTool.getIntRandom0to100());
		}
		
		/*while(!myLink.isEmpty()){
			Node node = myLink.deleteFirst();
			System.out.print("删除元素：");
			node.display();
			System.out.println();
		}*/
		
		myLink.display();
		Scanner c = new Scanner(System.in);
		int data = 0;
		while(c.hasNext() && ( (data = c.nextInt()) != -1)){
			myLink.delete(data);
			myLink.display();
		}
	}
	
	public static int input(){
		Scanner c = new Scanner(System.in);
		int data = 0;
		data = c.nextInt();
		return data;
	}

}
